package com.yh.workbill.ui.leaseInfo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.databinding.DataBindingUtil;
import okhttp3.internal.Util;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.yh.workbill.App;
import com.yh.workbill.R;
import com.yh.workbill.base.BaseActivity;
import com.yh.workbill.databinding.ActivityAddLeaseInfoBinding;
import com.yh.workbill.databinding.LayoutHeadBinding;
import com.yh.workbill.db.UnitCompany;
import com.yh.workbill.db.UnitCompanyDao;
import com.yh.workbill.model.UseCompany;
import com.yh.workbill.ui.leaseInfo.mvp.AddLease_Presenter;
import com.yh.workbill.ui.leaseInfo.mvp.AddLease_View;
import com.yh.workbill.utils.Utils;

import java.util.List;

public class AddLeaseInfoActivity extends BaseActivity implements AddLease_View {

    private ActivityAddLeaseInfoBinding binding;
    private LayoutHeadBinding layoutHead;
    private AddLease_Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_add_lease_info);
        layoutHead = binding.layoutHead;
    }


    @Override
    public void initJetPack(@Nullable Bundle savedInstanceState) {
        presenter = new AddLease_Presenter(this);
        this.getLifecycle().addObserver(presenter);
    }

    @Override
    public void initObserve(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initBaseSetting(@Nullable Bundle savedInstanceState) {
        layoutHead.setTitle("添加租借单位");
        layoutHead.llBlack.setOnClickListener(v -> finish());
    }

    @Override
    public void initClick(@Nullable Bundle savedInstanceState) {
        binding.btnAdd.setOnClickListener(v -> {
            String unitCompanyName = binding.etUnitCompanyName.getText().toString().trim();
            if (TextUtils.isEmpty(unitCompanyName)) {
                Utils.ShowToas(AddLeaseInfoActivity.this, "请输入租借单位名称");
                return;
            }
            presenter.add(unitCompanyName);
        });
    }

    @Override
    public void initDefaultMethod(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void addSuccess(String name) {
        Utils.ShowToas(this, "添加成功");
        binding.etUnitCompanyName.setText("");
        binding.etUnitCompanyName.setHint("请输租借单位");
        ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                UnitCompanyDao dao = App.appDataBase.getUnitCompanyDao();
                dao.add(new UnitCompany(name));
            }
        });
    }

    @Override
    public void fail(String msg) {
        Utils.ShowToas(this, msg);
    }

    @Override
    public void refresh() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void getData(List<UseCompany> data) {

    }

    @Override
    public void getDataFail(String msg) {

    }

    @Override
    public void delSuccess(String name) {

    }

    @Override
    public void delFail(String msg) {

    }

    @Override
    public void showTasot(String message) {
        Utils.ShowToas(this, message);
    }

    @Override
    public void showDialog(String content, boolean isCancelable) {
        showLoaddingDialog(content, isCancelable);
    }

    @Override
    public void hideDialog() {
        hideLoaddingDialog();
    }
}
